//
//  ViewController.m
//  相册
//
//  Created by apple on 16/12/16.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "ViewController.h"
#import <Photos/Photos.h>
#import <PhotosUI/PhotosUI.h>
#import<AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)NSMutableArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [NSMutableArray array];
    UICollectionViewFlowLayout *mylayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:mylayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
    UIImagePickerController *imagePickerController = [UIImagePickerController new];
    imagePickerController.delegate = self;
    //    imagePickerController.mediaTypes =UIImagePickerControllerMediaType
    //  imagePickerController.mediaTypes =UIImagePickerMediaTypeAny;
    //是否允许多选
    //    imagePickerController.
    //    imagePickerController.allowsMultipleSelection = YES;
    //    imagePickerController.showsNumberOfSelectedAssets = YES;
    //    imagePickerController.minimumNumberOfSelection = 3;
    //    imagePickerController.maximumNumberOfSelection = 6;
    // [imagePickerController.selectedAssets addObject:[PHAsset fetchAssetsWithOptions:nil].lastObject];
    
    
    PHFetchOptions *options = [[PHFetchOptions alloc]init];
    
    options.sortDescriptors=@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate"ascending:NO]];
    
    PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
    NSLog(@"%@",assetsFetchResults );
    //PHAsset *asset = assetsFetchResults.firstObject;
    for (PHAsset *set in assetsFetchResults) {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        _arr =[[NSMutableArray alloc]init];
        [[PHImageManager defaultManager] requestImageForAsset:set targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            //设置图片
            
            NSLog(@"--->%@",_arr);
//            if (_arr.count>0) {
//                _array =_arr;
//            }
            [_arr addObject:result];
            if (_arr.count>0) {
                NSLog(@"11");
                [_collectionView reloadData];

            }
            
        }];
        NSLog(@"-in,_arr->%@",_arr);
    }
   // NSLog(@"-out,_arr->%@",_arr);
//    if (_array.count > 0) {
//        _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
//        _imageView.image =_array[0];
//        [self.view addSubview:_imageView];
//    }
    // [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
}
//- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets
//{
//}
//
//    PHImageRequestOptions *options1 = [[PHImageRequestOptions alloc] init];
//    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
//
//    [[PHImageManager defaultManager]requestImageForAsset:asset targetSize:CGSizeMake(50, 50) contentMode:PHImageContentModeAspectFit  options:options1 resultHandler:^(UIImage *result, NSDictionary *info){
//        [self.arr addObject:result];
//    }];
//    NSLog(@"%@",asset);
// 获取所有资源的集合，并按资源的创建时间排序
//- (void)getImageFromIpc
//{
//    // 1.判断相册是否可以打开
//    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
//    // 2. 创建图片选择控制器
//    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
//    /**
//     typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
//     UIImagePickerControllerSourceTypePhotoLibrary, // 相册
//     UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取
//     UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿
//     }
//     */
//    // 3. 设置打开照片相册类型(显示所有相簿)
//    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    // 照相机
//    // ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
//    // 4.设置代理
//    ipc.delegate = self;
//    // 5.modal出这个控制器
//    [self presentViewController:ipc animated:YES completion:nil];
//}

//#pragma mark -- <UIImagePickerControllerDelegate>--
// 获取图片后的操作
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
//{
//    // 销毁控制器
//    [picker dismissViewControllerAnimated:YES completion:nil];
//
//    // 设置图片
//    self.imageView.image = info[UIImagePickerControllerOriginalImage];
//    NSLog(@"%@",_imageView);
//}
//- (void)getOriginalImages
//{
//    // 获得所有的自定义相簿
//    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//    // 遍历所有的自定义相簿
//    for (PHAssetCollection *assetCollection in assetCollections) {
//        [self enumerateAssetsInAssetCollection:assetCollection original:YES];
//    }
//
//    // 获得相机胶卷
//    PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
//    // 遍历相机胶卷,获取大图
//    [self enumerateAssetsInAssetCollection:cameraRoll original:YES];
//}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    _imageView.image =_arr[indexPath.row];
    [cell.contentView addSubview:_imageView];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
